|
|
|
|
|
|
|
caption is drawn in the center of the control. Note that several module-level variables are referenced in the Paint event; for example, the button's color is determined by the variable lngBackcolor, which holds the contents of the BackColor property. The following variables should be declared in the General Declarations section of the UserControl object: |
|
|
|
|
|
|
|
|
Dim lngForeColor As Long 'Button Foreground
Dim lngBackColor As Long 'Button Background
Dim bMouseDown As Boolean 'Is the mouse pressed? |
|
|
|
|
|
|
|
|
The module-level variables are necessary so that their values can be set from the property procedures of the control. The Boolean variable bMouseDown determines whether the button is drawn "raised" or "pressed." |
|
|
|
|
|
|
|
|
Figure 14.18 shows how the basic Color Button control looks on the form of a test project. |
|
|
|
|
|
|
|
|
Figure 14.18
Code in the Paint event draws the user
interface for the custom command button. |
|
|
|
|
|
|
|
|
The final step in creating the user interface is to set initial values for the button colors. To do so, place a few assignment statements in the Initialize event of the control, as follows: |
|
|
|
|
|
|
|
|
Private Sub UserControl_Initialize()
lngBackColor = vbCyan
lngForeColor = vbBlue
End Sub |
|
|
|
|
|
|
|
|
The preceding lines of code provide initial settings for the Paint event. |
|
|
|
|
|
|
|
|
Creating the Properties of the Button |
|
|
|
|
|
|
|
|
As stated previously, you can keep the design of the command button simple. The four key properties of the button are the BackColor, Caption, Font, and ForeColor properties. Three of these propertiesBackColor, Font, and ForeColorwill be tied to the corresponding properties of the user control. In addition, the BackColor and ForeColor properties will be stored as variables for use in the code. |
|
|
|
|
|
|
|
|
Using the ActiveX Control Interface Wizard, you can easily create the properties that you need. You have to add the Caption property as a Custom Member, as well as map the BackColor, Font, and ForeColor properties to those of the UserControl object. The wizard creates the Property Let and Property Get procedures for the four properties. Consider, for example, the Property Get procedure for the control's BackColor property: |
|
|
|
|
|
|
|
|
Public Property Get BackColor() As OLE_COLOR
BackColor = UserControl.BackColor
End Property |
|
|
|
|
|